#include "restoreandbackupdlg.h"
#include "ui_restoreandbackupdlg.h"
#include "qmessagebox.h"
#include "communicatedelegate.h"

RestoreAndBackupDlg::RestoreAndBackupDlg(int styleId,CommunicateNormalDelegate *communicateNormalIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RestoreAndBackupDlg)
{
    ui->setupUi(this);
    communicateNormal=communicateNormalIn;
        move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif
}

void RestoreAndBackupDlg::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        setGeometry(0,0,600,450);
        ui->listWidget->setGeometry(20,290,420,100);
        ui->pushButton_4->setGeometry(600-110-20,450-50-10,110,50);
        break;
    default:
        break;
    }
}

RestoreAndBackupDlg::~RestoreAndBackupDlg()
{
    delete ui;
}

void RestoreAndBackupDlg::on_pushButton_clicked()
{
    int backupType=ui->comboBox_2->currentIndex();
    int diskType=ui->comboBox->currentIndex();
    int robotId=ui->comboBox_3->currentIndex()+1;

    std::string fileName=ui->lineEdit->text().toStdString();
    int returnFlag;


    communicateNormal->backupFile(backupType,diskType,fileName,robotId,returnFlag);
    if(0==returnFlag)
    {
        QMessageBox::information(NULL, "information", "backup成功！", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "information", "backup失败！", QMessageBox::Yes );
    }
}

void RestoreAndBackupDlg::on_pushButton_2_clicked()
{
    QMessageBox message(QMessageBox::NoIcon, "Show Qt", "确定要恢复选中文件吗，旧文件将会被覆盖?"
                        , QMessageBox::Yes | QMessageBox::No, NULL);
    if(message.exec() == QMessageBox::Yes)
    {
        int backupType=ui->comboBox_2->currentIndex();
        int diskType=ui->comboBox->currentIndex();
        int robotId=ui->comboBox_3->currentIndex();

        std::string fileName=ui->lineEdit->text().toStdString();
        int returnFlag;


        communicateNormal->restoreFile(backupType,diskType,fileName,robotId,returnFlag);
        if(0==returnFlag)
        {
            QMessageBox::information(NULL, "information", "restore成功！", QMessageBox::Yes );
        }
        else
        {
            QMessageBox::information(NULL, "information", "restore失败！", QMessageBox::Yes );
        }
    }
}



void RestoreAndBackupDlg::on_pushButton_3_clicked()
{
    ui->listWidget->clear();
    int backupType=ui->comboBox->currentIndex();
    std::vector<std::string> fileList;
    communicateNormal->getFileList(backupType,fileList);
    for(int i=0;i<fileList.size();i++)
    {
        ui->listWidget->addItem(QString::fromStdString(fileList[i]));
    }
}

void RestoreAndBackupDlg::on_pushButton_4_clicked()
{
    hide();
}

void RestoreAndBackupDlg::on_listWidget_clicked(const QModelIndex &index)
{
    ui->lineEdit->setText(ui->listWidget->currentItem()->text());

}

void RestoreAndBackupDlg::on_pushButton_restore_original_clicked()
{
    QMessageBox message(QMessageBox::NoIcon, "Show Qt", "确定要恢复出厂状态吗，现有信息将会丢失?"
                        , QMessageBox::Yes | QMessageBox::No, NULL);
    if(message.exec() == QMessageBox::Yes)
    {
        int backupType=6;
        int diskType=0;
        int robotId=ui->comboBox_3->currentIndex();

        std::string fileName="controller_original.zip";
        int returnFlag;

//        p_powerManger->restoreFile(userInfo,backupType,diskType,fileName,robotId,returnFlag);
        communicateNormal->restoreFile(backupType,diskType,fileName,robotId,returnFlag);
        if(0==returnFlag)
        {
            QMessageBox::information(NULL, "information", "restore成功！", QMessageBox::Yes );
        }
        else
        {
            QMessageBox::information(NULL, "information", "restore失败！", QMessageBox::Yes );
        }
    }
}
